import * as constants from './constants'
import api from '../../../api/api'
import Immutable from 'immutable';

import { message } from 'antd';

const changeLive = (result) => ({
	type: constants.CHANGE_LIVE,
	value: Immutable.fromJS(result)
})

const changeUrl = (result) => ({
  type: constants.CHANGE_URL,
	value: Immutable.fromJS(result)
})

const changeRate = (result) => ({
  type: constants.CHANGE_RATE,
  value: Immutable.fromJS(result)
})

const changeComment = (result) => ({
  type: constants.CHANGE_COMMENT,
  value: Immutable.fromJS(result)
})

const changeLiveImg = (result) => ({
  type: constants.CHANGE_LIVEIMG,
  value: Immutable.fromJS(result)
})

const changeLocalImg = (result) => ({
  type: constants.CHANGE_LOCALIMG,
  value: Immutable.fromJS(result)
})

export const changeWorkHome = (result) => ({
  type: constants.CHANGE_WORKHOME,
  value: Immutable.fromJS(result)
})

export const getVideoItem = (params) => (dispatch) => {
  api.getVideoUrl(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeLive(res.data))
      // 下载课件
      dispatch(downloadCourse({lessonId: res.data.lessonId,period: res.data.period}))
      // 获取评分
      dispatch(getRate({
        id: res.data.lessonId,
        type: 1
      }))

      dispatch(getHomeWork({
        lessonId: res.data.lessonId,
        period: res.data.period
      }))
      // 获取评论
      let obj = {
        page: 1,
        pageSize: 15
      }
      res.data.type === 'JoyActivity' ? obj.actId = res.data.actId : obj.videoId = res.data.videoId
      dispatch(getComment(obj))
      // 获取直播课件
      if(res.data.type === 'JoyActivity') {
        dispatch(getLiveImg({
          actId: res.data.actId
        }))
      }else {
        dispatch(getLocalImg({
          lessonId: res.data.lessonId,
          period: res.data.period
        }))
      }
    }
  })
}

export const downloadCourse = (params) => (dispatch) => {
  api.downloadCourse(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeUrl(res.data.fileUrl))
    }
  })
}

export const getRate = (params) => (dispatch) => {
  api.getRate(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeRate(res.data))
    }
  })
}

export const addRate = (params) => (dispatch) => {
  api.addRate(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeRate(res.data))
    }
  })
}

export const getComment = (params) => (dispatch) => {
  api.getComment(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeComment(res.data))
    }
  })
}

export const addComment = (params) => (dispatch) => {
  api.addComment(params).then(res => {
    if(res.code === 20000) {
      message.success('添加成功!')
    }
  })
}

export const reply = (params) => (dispatch) => {
  api.reply(params).then(res => {
    if(res.code === 20000) {
      message.success('添加成功!')
    }
  })
}

export const getLiveImg = (params) => (dispatch) => {
  api.getLiveImg(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeLiveImg(res.data))
    }
  })
}

export const getLocalImg = (params) => (dispatch) => {
  api.getLocalImg(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeLocalImg(res.data))
    }
  })
}

export const getCommentByPhone = (params) => (dispatch) => {
  api.getCommentByPhone(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeComment(res.data))
    }
  })
}


// 给手机使用
export const getVideoByPhone = (params) => (dispatch) => {
  let obj = {}
  api.getVideoUrl(params).then(res => {
    res.data.type === 'JoyActivity' ? obj.actId = res.data.actId : obj.videoId = res.data.videoId
    if(res.code === 20000) {
      dispatch(changeLive(res.data))
      if(res.data.isTrial === 'true') {
      }else {
        dispatch(getCommentByPhone(obj))
        if(res.data.type === 'JoyActivity') {
          dispatch(getLiveImg({
            actId: res.data.actId
          }))
        }
      }
    }
  })
}

// 获取作业列表
export const getHomeWork = (params) => (dispatch) => {
  api.getHomeWorks(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeWorkHome(res.data))
    }
  })
}

export const saveHomeWork = (params) => (dispatch) => {
  api.saveAnswer(params).then(res => {
    if(res.code === 20000) {
      
    }
  })
}